iT邦幫忙

2025 iThome 鐵人賽

DAY 20
0
Modern Web

AI 驅動的 Web 資安新時代系列 第 20

Day20 - Agent × RAG:即時查詢最新 CVE 與 Exploit POC

  • 分享至 

  • xImage
  •  

在資安領域,「時間差」往往決定攻防勝負。

攻擊者 會第一時間找到 Exploit POC(Proof of Concept),並嘗試利用;

防禦者 若 lag 幾天才知道 CVE,就可能造成大規模入侵。

透過 Agent × RAG,我們可以建立「即時漏洞追蹤系統」,讓 AI 幫你查詢最新 CVE,並比對是否已有公開 POC。


工作流程 (Toggle List)

  • 收集 (Collector Agent)
    • 來源:NVD、CVE.org、Exploit-DB、GitHub
    • 透過排程自動抓取最新漏洞資料
  • 索引 (RAG Pipeline)
    • 把 CVE 描述、CVSS 分數、POC 連結放入向量資料庫(FAISS / Pinecone)
  • 查詢 (User Agent)
    • 使用自然語言問:「Apache Struts 最新漏洞?」
    • RAG 幫你召回相關 CVE 文件
  • 分析 (AI 判斷)
    • 判斷是否已有 Exploit POC
    • 產生「風險摘要 + 修補建議」

Server - 模擬 CVE API

1、初始化 Express 與中介層

import express from "express";
const app = express();

2、模擬 CVE API:/api/cve

  • 建立 /api/cve endpoint 回傳範例資料,方便本地測試 Agent。
  • 生產環境可替換為排程抓取 NVD 或同步資料庫的實作。
// 提供模擬 CVE API
app.get("/api/cve", (req, res) => {
   res.json([
      {
         cve: "CVE-2025-12345",
         desc: "Apache Struts Remote Code Execution",
         cvss: 9.8,
         poc: "https://github.com/exploit-db/12345"
      }
   ]);
});

3、啟動伺服器

app.listen(3000, () => console.log("伺服器啟動中: http://localhost:3000"));

Agent

1、載入套件與初始化模型

  • 載入 node-fetch 來呼叫本地 API,並用假想的 GoogleGenerativeAI SDK 初始化 Gemini 模型。
  • 請把 GEMINI_API_KEY 放在 .env
import fetch from "node-fetch";
import { GoogleGenerativeAI } from "@google/generative-ai";
import 'dotenv/config';

const API_KEY = process.env.GEMINI_API_KEY;
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });

2、查詢並分析 CVE

  1. 從本地 /api/cve 取得 CVE 清單。
  2. 以關鍵字(描述包含)與 CVSS 閾值(>=9.0)做簡單篩選。
  3. 組成 prompt 送入 LLM 要求生成結構化報告,最後印出模型回應。

注意:篩選用 .includes 為示範,正式系統建議改用向量檢索(RAG)或更嚴謹的文字相似度比對;model.generateContent 請依你實際使用的 SDK 做參數/回傳格式的調整。

async function queryCVE(keyword) {
   const res = await fetch("http://localhost:3000/api/cve");
   const data = await res.json();

   // 篩選關鍵字 & 高風險漏洞
   const filtered = data.filter(item => 
      item.desc.includes(keyword) && item.cvss >= 9.0
   );

   const prompt = `
   以下是最新 CVE 資料:
   ${JSON.stringify(filtered, null, 2)}

   請幫我整理成報告,包含:
   1. CVE 編號
   2. 漏洞描述
   3. CVSS 分數
   4. 是否已有 Exploit POC
   5. 修補建議
   `;

   const result = await model.generateContent(prompt);
   console.log(result.response.text());
}

queryCVE("Apache Struts");

上一篇
Day19 - AI Agent 幫我排程弱點掃描與回報
下一篇
Day21 - AI Agent 產生安全報告:從技術到管理摘要
系列文
AI 驅動的 Web 資安新時代22
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言